package maingoal;

import java.io.BufferedReader;
import java.io.FileReader;

import istar.agent.IstarJadeAgent;
import istar.impl.AbstractMainGoal;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.OneShotBehaviour;
import log.Log;

public class ProfitBeAchieved extends AbstractMainGoal {
	
	private boolean log = true;
	//private boolean deliberateCondition = false;
	private int simulationStep = -1;
	private int simulationTotalSteps = 7963;
	private String pathToSimulationFile = "";
	
	public ProfitBeAchieved(IstarJadeAgent owner) {
		super("ProfitBeAchieved", owner);
	}

	public ProfitBeAchieved() {
		super("ProfitBeAchieved");
	}


	@Override
	public Behaviour getMainBehaviour() {
		return new RunSimulation(this);
		
	}
	
	private class RunSimulation extends OneShotBehaviour{
		AbstractMainGoal mainGoal;
		int step=0;
		
		public RunSimulation(AbstractMainGoal _mainGoal){
			super();
			this.step = 0;
			this.mainGoal = _mainGoal;
		}
		
		@Override
		public void action() {
			switch (step) {
			case 0:
				mainGoal.clear();
				System.out.println(myAgent.getName()+ " - " + this.getBehaviourName() + " is executing step=0");
				step=1;
				break;
			case 1:

				simulationStep++;
				
				String ticket = ((IstarJadeAgent)myAgent).getNextTicket();
				mainGoal.setExecutionNumber(ticket);
				mainGoal.deliberate();
				log(myAgent.getName() +": executing. Ticket="+ mainGoal.getExecutionNumber());
				
				String simulationTime ="";
				String testData="";
				Double brokerageFee = null;
				Double highStrike = null;
				Double lowStrike = null;
				try {
					BufferedReader in = new BufferedReader(new FileReader(((IstarJadeAgent)myAgent).configurationPath));
				    String str;
				    while ((str = in.readLine()) != null) {
				        if (str.startsWith("TestDataFile=")){
				        	testData = str.substring(13);
				        }
				        if (str.startsWith("BrokerageFee=")){
				        	brokerageFee = new Double (str.substring(13));
				        }
				        if (str.startsWith("LowStrike=")){
				        	lowStrike = new Double (str.substring(10));
				        }
				        if (str.startsWith("HighStrike=")){
				        	highStrike = new Double (str.substring(11));
				        }
				        
				    }
				    in.close();
				    
				    Double spread = new Double(highStrike) - new Double(lowStrike);
				    
				    ((IstarJadeAgent)myAgent).setKnowledgeObject(ticket, "BrokerageFee", brokerageFee);
				    ((IstarJadeAgent)myAgent).setKnowledgeObject(ticket, "LowStrike", lowStrike);
				    ((IstarJadeAgent)myAgent).setKnowledgeObject(ticket, "HighStrike", highStrike);
				    ((IstarJadeAgent)myAgent).setKnowledgeObject(ticket, "Spread", spread);
				    
				    BufferedReader testDataFile = new BufferedReader(new FileReader(testData));
				    String simulationLine="";
				    int i=0;
				    while (i<=simulationStep){
				    	simulationLine = testDataFile.readLine();
				    	i++;
				    }
				    simulationTime = simulationLine.substring(1, simulationLine.indexOf(";")-1);
				    
				} catch (Exception e) {
					e.printStackTrace();
				}
				//mainGoal.clear();
				//changed here
				//mainGoal.getContent().add("<simulation_step="+ simulationStep +";simulation_time="+simulationTime+">");
				
				((IstarJadeAgent)myAgent).setParameter(ticket, mainGoal.getName(),"SimulationTime="+ simulationTime +";SimulationStep="+simulationStep);
				((IstarJadeAgent)myAgent).setKnowledgeObject(ticket, "SimulationTime", simulationTime);
				((IstarJadeAgent)myAgent).setKnowledgeObject(ticket, "SimulationStep", simulationStep);
				
				step=0;
			default:
				break;
			}
		}

		@Override
		public int onEnd() {
			int result = 0;
			if (mainGoal.deliberateCondition()){
				//this.step = 0;
				mainGoal.undeliberate();
				if (this.mainGoal.achieved())
					result = 2;
				else
					result = 1;
			}
			System.out.println(getBehaviourName() + " executou onEnd() com result=" + result);
			
			return result;
		}
		
		private void log(String _phrase){
			if (log){
				((IstarJadeAgent)myAgent).log( _phrase);
			}
		}
		
	}
	
}
